// =======================================================================
// 
//       Filename:  parser.hpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  25.03.2010 21:18:50
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================


#ifndef  PARSER_INC
#define  PARSER_INC
#include "../types/string.hpp"
#include "../utils/citerator.hpp"
#include <stdexcept>
#include <map>
#include <list>
#include <boost/shared_ptr.hpp>

namespace webplodder {
    // Forward declaration
    namespace types {
        class Object;
    }

    namespace templatefile {    
        // Forward declaration
        class File;
        class ScriptElement;
        class VarElement;
        class Element;

        /**
         * This class parses a template file
         * and interprets commands and scripts on the fly.
         * @brief This class parses a template file.
         */
        class Parser {
            public:
                // Types
                enum CacheMode {
                    CACHE_NOTHING,
                    CACHE_SCRIPT,
                    CACHE_ALL
                };

            public:
                /**
                 * @brief Constructs default Parser.
                 */
                Parser();

                /**
                 * @brief Launches the parsing process.
                 * @throw std::logic_error Thrown if a script is detected
                 *                         and no rule defined
                 * @throw SyntaxError Thrown if an syntax error is detected.
                 */
                void parse(templatefile::File& templateFile);

                /**
                 * @brief Returns the underlaying CIterator.
                 */
                utils::CIterator* iterator();

                /**
                 * @brief Sets the current cache mode.
                 */
                void setCacheMode(CacheMode mode);

            private:
                void safelyIncr();
                void skipWhiteSpace();
                boost::shared_ptr<templatefile::ScriptElement> parseScript(
                        templatefile::File& templateFile);
                boost::shared_ptr<templatefile::VarElement> parseVar(
                        templatefile::File& templateFile);
                boost::shared_ptr<templatefile::Element> 
                    parseCmd(templatefile::File& templateFile);
                boost::shared_ptr<types::String>
                    parseString();
                boost::shared_ptr<types::Object>
                    parseNumber();

                utils::CIterator mIterator;
                int mLine;
                CacheMode mCacheMode;
        };
    }
}
#endif   // ----- #ifndef PARSER_INC  -----
